//
// Created by jsjxsy on 2022/9/7.
// 菱形继承问题s
//
#include <iostream>
#include <string>
using namespace std;

class Animal {
 public:
  virtual void speak() = 0;
  virtual ~Animal() = 0;
};
Animal::~Animal() {
  cout << "~Animal" << endl;
}
class Cat : public Animal {
 public:
  Cat(const string &name) {
    p_name = new string(name);
  }
  virtual void speak() {
    cout << *p_name << " cat speaking" << endl;
  }
  string *p_name;
  ~Cat() {
    if (p_name != nullptr) {
      delete p_name;
      p_name = nullptr;
    }
    cout << "~Cat" << endl;
  }

};
int main() {
  Animal *a = new Cat("tom");
  a->speak();
  delete a;

  Cat c2("small");
  Animal *a2;
  a2 = &c2;
  a2->speak();

  //相当于在栈区构建一个Animal对象
  //但是 由于存在纯虚函数是不能构建对象的
  //Animal a3 = c2;
  return 0;
}

